package com.hunttown.mes.impl;

import com.hunttown.mes.rpc.domain.TransportPurchaseDTO;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportPurchaseRpcService;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseDTOQuery;
import com.hunttown.mes.service.TransportPurchaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.TransportPurchaseConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:05
 */
@Service
public class TransportPurchaseRpcServiceImpl implements TransportPurchaseRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportPurchaseRpcServiceImpl.class);

    private final TransportPurchaseService transportPurchaseService;

    @Autowired
    public TransportPurchaseRpcServiceImpl(TransportPurchaseService transportPurchaseService) {
        this.transportPurchaseService = transportPurchaseService;
    }

    @Override
    public TransportPurchaseDTO insert(TransportPurchaseDTO transportPurchaseDTO) {
        try {
            return convert(transportPurchaseService.insert(convert(transportPurchaseDTO)));
        } catch (Exception e) {
            logger.error("TransportPurchase create failed.", e);
            throw new RuntimeException("TransportPurchase create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportPurchaseDTO transportPurchaseDTO) {
        try {
            return transportPurchaseService.updateInfoById(convert(transportPurchaseDTO));
        } catch (Exception e) {
            logger.error("TransportPurchase update by id failed.", e);
            throw new RuntimeException("TransportPurchase update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportPurchaseDTOQuery transportPurchaseDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseDTOQuery.getW_id() != null && transportPurchaseDTOQuery.getW_id() > 0) {
                query.put("w_id", transportPurchaseDTOQuery.getW_id());
            }

            if (transportPurchaseDTOQuery.getIds() != null && !transportPurchaseDTOQuery.getIds().equals("")) {
                query.put("ids", transportPurchaseDTOQuery.getIds());
            }

            if (transportPurchaseDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseDTOQuery.getStopFlag());
            }

            if (transportPurchaseDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseDTOQuery.getDeleteFlag());
            }

            return transportPurchaseService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportPurchase update by query failed.", e);
            throw new RuntimeException("TransportPurchase update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseDTO selectById(Integer id) {
        try {
            return convert(transportPurchaseService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportPurchase select by id failed.", e);
            throw new RuntimeException("TransportPurchase select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseDTO selectObjByQuery(TransportPurchaseDTOQuery transportPurchaseDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseDTOQuery.getStopFlag());
            }

            if (transportPurchaseDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseDTOQuery.getDeleteFlag());
            }

            return convert(transportPurchaseService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportPurchase select obj by query failed.", e);
            throw new RuntimeException("TransportPurchase select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportPurchaseDTO> getListForPage(TransportPurchaseDTOQuery transportPurchaseDTOQuery) {
        Query query = Query.page(transportPurchaseDTOQuery.getPn(), transportPurchaseDTOQuery.getPageSize());

        if (transportPurchaseDTOQuery.getId() != null && transportPurchaseDTOQuery.getId() > 0) {
            query.put("id", transportPurchaseDTOQuery.getId());
        }

        if (transportPurchaseDTOQuery.getIds() != null && !transportPurchaseDTOQuery.getIds().equals("")) {
            query.put("ids", transportPurchaseDTOQuery.getIds());
        }

        if (transportPurchaseDTOQuery.getEntId() != null && transportPurchaseDTOQuery.getEntId() > 0) {
            query.put("entId", transportPurchaseDTOQuery.getEntId());
        }

        if (transportPurchaseDTOQuery.getCatalogId() != null && transportPurchaseDTOQuery.getCatalogId() != -9999) {
            query.put("catalogId", transportPurchaseDTOQuery.getCatalogId());
        }

        if (StringUtils.isNotBlank(transportPurchaseDTOQuery.getLikeName())) {
            query.put("likeName", transportPurchaseDTOQuery.getLikeName());
        }

        if (transportPurchaseDTOQuery.getPurchaseType() != null) {
            query.put("purchaseType", transportPurchaseDTOQuery.getPurchaseType());
        }

        if (StringUtils.isNotBlank(transportPurchaseDTOQuery.getShowDate())) {
            query.put("showDate", transportPurchaseDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDTOQuery.getStartDate())) {
            query.put("startDate", transportPurchaseDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDTOQuery.getEndDate())) {
            query.put("endDate", transportPurchaseDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDTOQuery.getOrderBy())) {
            query.put("orderBy", transportPurchaseDTOQuery.getOrderBy());
        }

        if (transportPurchaseDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportPurchaseDTOQuery.getStopFlag());
        }

        if (transportPurchaseDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportPurchaseDTOQuery.getDeleteFlag());
        }

        List<TransportPurchaseDTO> list = convert(transportPurchaseService.selectByQuery(query));
        Page<TransportPurchaseDTO> page = Page.from(transportPurchaseDTOQuery.getPn(), transportPurchaseDTOQuery.getPageSize(), list);
        page.setTotalItems(transportPurchaseService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportPurchaseService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportPurchase delete failed.", e);
            throw new RuntimeException("TransportPurchase delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> selectSimilarPurchase(String similar, Integer purchaseType) {
        try {
            Query query = Query.page(1, 20);

            if (StringUtils.isNotBlank(similar)) {
                query.put("likeName", similar.trim());
            }

            if (purchaseType != null && purchaseType > 0) {
                query.put("purchaseType", purchaseType);
            }

            return transportPurchaseService.selectSimilarPurchase(query);

        } catch (Exception e) {
            logger.error("TransportPurchase select similar user failed.", e);
            throw new RuntimeException("TransportPurchase similar user failed, Please order your code.", e);
        }
    }
}